package RobertsCoalition.scoring;

import java.util.List;

import multiarray.MultiArray;

import population.Population;
import RobertsCoalition.CoalitionInterface;
import RobertsCoalition.CoalitionWithInfo;

public class AltruismScore implements CoalitionScore{

	Population p;
	MultiArray<MultiArray<Float>> altruismArrays;
	float alpha;
	float pie;
	List<int[]> sectors;
	float populationSize;
	List<AgentAltruism> agentAltruismList;
	
	public AltruismScore(Population p,
			MultiArray<MultiArray<Float>> altruismArrays,
			float alpha
	){
		this.altruismArrays = altruismArrays;
		this.p = p;
		this.alpha = alpha;
		populationSize = p.getTotalModulatedSize();
		this.agentAltruismList = p.getAgentAltruismList();
	}
	
	@Override
	public float getCoalitionScore(
			CoalitionWithInfo coalitionWithInfo) {
		CoalitionInterface coalition = coalitionWithInfo.getCoalition();
		List<int[]> sectors = coalition.getSectorsInvolved();
		
		float coalitionPower = coalitionWithInfo.getCoalitionModulatedSize();
		float powerRatio = coalitionPower/populationSize;
		if(powerRatio >= .5) pie = alpha; else pie = 1 - alpha;
		

		float coalitionUtility = 0;
		for(int[] agentSector : sectors){
			coalitionUtility += p.getSectorModulatedPopulationSize(agentSector)*
				getAgentUtility(agentSector, sectors);
		}
		
		return 0;
	}
	
	private float getAgentUtility(int[] agentSector, List<int[]> sectors){
		float personalUtility = pie/p.getSectorModulatedPopulationSize(agentSector);
		float groupUtility = 0;		
		for(int[] targetSector : sectors){
			float altruism = altruismArrays.getElementAtIndex(agentSector).getElementAtIndex(targetSector);
			groupUtility += altruism * p.getSectorModulatedPopulationSize(targetSector)*
			pie/populationSize;
		}		
		return personalUtility + groupUtility;
	}
	
	private void indexAgentAltruisms(){
		for(AgentAltruism agentAltruism : agentAltruismList){
			
		}
	}

}
